<?php

namespace app\admin\controller;

class Course extends Base
{
    //课程列表
    public function course_list()
    {
        $cou = model('common/Course') -> paginate(5);
        $data = [
            'cou' => $cou
        ];
        $this -> assign($data);
        return view();
    }

    //课程添加
    public function course_add()
    {
        if (request()->isAjax()){
            $data = input('post.');
            $data['img'] = request()->file('myFile');
            $result = model('common/Course') -> course_add($data);
            if ($result == 1){
                $this -> success('添加成功！','admin/course/course_list');
            } else {
                $this -> error($result);
            }
        }
        return view();
    }

    //课程编辑
    public function course_edit()
    {
        if (request()->isajax()){
            $data = input('post.');
            $data['imgs'] = request() -> file('myFile');
            $result = model('common/Course') -> course_edit($data);
            if ($result == 1){
                $this -> success('更新成功！','admin/course/course_list');
            } else {
                $this -> error($result);
            }
        }
        $cou = model('common/Course') -> find(input('post.id'));
        $data = [
            'cou' =>$cou
        ];
        $this -> assign($data);
        return view();
    }

    //课程删除
    public function course_del()
    {
        if (request() -> isAjax()){
            $cou = model('common/Course') -> find(input('post.id'));
            $result = $cou -> delete();
            if ($result == 1){
                $this -> success('删除成功！','admin/course/course_list');
            } else {
                $this -> error($result);
            }
        } else {
            return '非法请求';
        }
    }

}
